﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using Telerik.WinControls.UI;
using System.IO;
using Bitis_KeToanThue_Pro_DungChung.Common;
using Bitis_KeToanThue_Pro_QuanTri.AccessDatabase;
using Telerik.WinControls.Data;
using System.Reflection;
namespace Bitis_KeToanThue_Pro_QuanTri.Form
{
    public partial class frmViewMenu : Telerik.WinControls.UI.RadForm
    {
        PhanNhomMenu pn = new PhanNhomMenu();
        public frmViewMenu(int nhom)
        {
            InitializeComponent();
            taomenu(nhom);
        }
        public void taomenu(int nhom)
        {
            DataTable dt = new DataTable();
            dt = pn.loadmenushowtest(nhom);

            //menu gồm 5 cấp
            if (dt.Rows.Count > 0)
            {


                radMenu1.BeginInit();
                var menucap0 = from myRowmenucap0 in dt.AsEnumerable()
                               where myRowmenucap0.Field<int>("CapMenu") == 0
                               select myRowmenucap0;
                DataTable dtmenucap0 = new DataTable();
                try
                {
                    dtmenucap0 = menucap0.CopyToDataTable();
                }
                catch
                {
                    dtmenucap0.Clear();
                }
                //load menu cap 0 (menu root)
                if (dtmenucap0.Rows.Count > 0)
                {
                    for (int i = 0; i < dtmenucap0.Rows.Count; i++)
                    {
                        radMenu1.Items.Add(new RadMenuItem(dtmenucap0.Rows[i]["MenuName"].ToString()));
                        RadMenuItem radmenuitemcap1 = (RadMenuItem)radMenu1.Items[i];
                        radmenuitemcap1.Name = dtmenucap0.Rows[i]["ChuoiMenu"].ToString();
                        radmenuitemcap1.Image = ClassXuLyChung.GetIamgeToDataBase(dtmenucap0.Rows[i]["Hinh"]);
                        var menucap1 = from myRowmenucap1 in dt.AsEnumerable()
                                       where myRowmenucap1.Field<int>("CapMenu") == 1 && myRowmenucap1.Field<int>("MenuParent") == int.Parse(dtmenucap0.Rows[i]["ID"].ToString())
                                       select myRowmenucap1;
                        DataTable dtmenucap1 = new DataTable();

                        try
                        {
                            dtmenucap1 = menucap1.CopyToDataTable();
                        }
                        catch
                        {
                            dtmenucap1.Clear();
                        }
                        if (dtmenucap1.Rows.Count > 0)
                        {
                            RadMenuItem item = (RadMenuItem)radMenu1.Items[i];
                            for (int j = 0; j < dtmenucap1.Rows.Count; j++)
                            {
                                item.Items.Add(new RadMenuItem(dtmenucap1.Rows[j]["MenuName"].ToString()));
                                RadMenuItem radmenuitemcap2 = (RadMenuItem)item.Items[j];
                                radmenuitemcap2.Name = dtmenucap1.Rows[j]["ChuoiMenu"].ToString();
                                radmenuitemcap2.Image = ClassXuLyChung.GetIamgeToDataBase(dtmenucap1.Rows[j]["Hinh"]); 
                                var menucap2 = from myRowmenucap2 in dt.AsEnumerable()
                                               where myRowmenucap2.Field<int>("CapMenu") == 2 && myRowmenucap2.Field<int>("MenuParent") == int.Parse(dtmenucap1.Rows[j]["ID"].ToString())
                                               select myRowmenucap2;
                                DataTable dtmenucap2 = new DataTable();

                                try
                                {
                                    dtmenucap2 = menucap2.CopyToDataTable();
                                }
                                catch
                                {
                                    dtmenucap2.Clear();
                                }
                                if (dtmenucap2.Rows.Count > 0)
                                {
                                    RadMenuItem item2 = (RadMenuItem)item.Items[j];
                                    for (int k = 0; k < dtmenucap2.Rows.Count; k++)
                                    {
                                        item2.Items.Add(new RadMenuItem(dtmenucap2.Rows[k]["MenuName"].ToString()));
                                        RadMenuItem radmenuitemcap3 = (RadMenuItem)item2.Items[k];
                                        radmenuitemcap3.Name = dtmenucap2.Rows[k]["ChuoiMenu"].ToString();
                                        radmenuitemcap3.Image = ClassXuLyChung.GetIamgeToDataBase(dtmenucap2.Rows[k]["Hinh"]); 
                                        var menucap3 = from myRowmenucap3 in dt.AsEnumerable()
                                                       where myRowmenucap3.Field<int>("CapMenu") == 3 && myRowmenucap3.Field<int>("MenuParent") == int.Parse(dtmenucap2.Rows[k]["ID"].ToString())
                                                       select myRowmenucap3;
                                        DataTable dtmenucap3 = new DataTable();

                                        try
                                        {
                                            dtmenucap3 = menucap3.CopyToDataTable();
                                        }
                                        catch
                                        {
                                            dtmenucap3.Clear();
                                        }
                                        if (dtmenucap3.Rows.Count > 0)
                                        {
                                            RadMenuItem item3 = (RadMenuItem)item2.Items[k];
                                            for (int h = 0; h < dtmenucap3.Rows.Count; h++)
                                            {
                                                item3.Items.Add(new RadMenuItem(dtmenucap3.Rows[h]["MenuName"].ToString()));
                                                RadMenuItem radmenuitemcap4 = (RadMenuItem)item3.Items[h];
                                                radmenuitemcap4.Name = dtmenucap3.Rows[h]["ChuoiMenu"].ToString();
         
                                                radmenuitemcap4.Image = ClassXuLyChung.GetIamgeToDataBase(dtmenucap3.Rows[h]["Hinh"]); 
                                     
                                                var menucap4 = from myRowmenucap4 in dt.AsEnumerable()
                                                               where myRowmenucap4.Field<int>("CapMenu") == 4 && myRowmenucap4.Field<int>("MenuParent") == int.Parse(dtmenucap3.Rows[h]["ID"].ToString())
                                                               select myRowmenucap4;
                                                DataTable dtmenucap4 = new DataTable();

                                                try
                                                {
                                                    dtmenucap4 = menucap4.CopyToDataTable();
                                                }
                                                catch
                                                {
                                                    dtmenucap4.Clear();
                                                }
                                                if (dtmenucap4.Rows.Count > 0)
                                                {
                                                    RadMenuItem item4 = (RadMenuItem)item3.Items[h];
                                                    for (int n = 0; n < dtmenucap4.Rows.Count; n++)
                                                    {
                                                        item4.Items.Add(new RadMenuItem(dtmenucap4.Rows[n]["MenuName"].ToString()));
                                                        RadMenuItem radmenuitemcap5 = (RadMenuItem)item4.Items[n];
                                                        radmenuitemcap5.Name = dtmenucap4.Rows[n]["ChuoiMenu"].ToString();
                                                        radmenuitemcap5.Image = ClassXuLyChung.GetIamgeToDataBase(dtmenucap4.Rows[n]["Hinh"]); 

                                                        ((RadMenuItem)item4.Items[n]).Click += new EventHandler(item_Click);

                                                    }
                                                }
                                                else
                                                {
                                                    ((RadMenuItem)item3.Items[h]).Click += new EventHandler(item_Click);
                                                }
                                            }
                                        }
                                        else
                                        {
                                            ((RadMenuItem)item2.Items[k]).Click += new EventHandler(item_Click);
                                        }

                                    }

                                }
                                else
                                {
                                    ((RadMenuItem)item.Items[j]).Click += new EventHandler(item_Click);
                                }
                            }
                        }
                        else
                        {
                            ((RadMenuItem)radMenu1.Items[i]).Click += new EventHandler(item_Click);
                        }
                    }
                }

                radMenu1.EndInit();


            }

        }
        private void item_Click(object sender, EventArgs e)
        {
            string name = "";
            RadMenuItem item = ((RadMenuItem)sender);
            name = item.Name;
            RadMessageBox.Show(name);
         //   MessageBox.Show(name.Substring(0,name.IndexOf("!!!!!")));
         //   MessageBox.Show(name.Substring(name.IndexOf("!!!!!")+5, name.IndexOf("&&&&&") - name.IndexOf("!!!!!")-5));
           // MessageBox.Show(name.Substring(name.IndexOf("&&&&&")+5, name.Length - name.IndexOf("&&&&&")-5));
     //       ClassXuLyChung.showformbymenuloaddulieudatabase(name.Substring(name.IndexOf("!!!!!") + 5, name.IndexOf("&&&&&") - name.IndexOf("!!!!!") - 5),
            //    name.Substring(0, name.IndexOf("!!!!!")));
          //  ClassXuLyChung.showformbymenuloaddulieudatabase("frmDanhMucMenu", "Bitis_KeToanThue_Pro_QuanTri.Form.frmDanhMucMenu");
          
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
       

      
    }
}
